package com.qf.boot2203.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
public class FileUpLoadController {

    @Value("${qfsy.fileupload.path}")
    String filePath;

    @PostMapping("/upload")
    public String upload(MultipartFile file) throws IOException {
        //1.生成一个唯一的文件名

        String prefix = UUID.randomUUID().toString();

        String originalFilename = file.getOriginalFilename();

        //获取存放jar的相同路径

        String dir = System.getProperty("user.dir");

        //2.拼装文件在服务器端的绝对路径路径

        //String finalFileName = filePath + prefix + "-" + originalFilename;

        String fianldirpath = dir + File.separator + "src\\main\\resources\\img\\" + prefix + "-" + originalFilename;

        //3存文件

        //file.transferTo(new File(finalFileName));
        file.transferTo(new File(fianldirpath));

        return "ok";
    }
}
